<template>
  <div class="category-page">
    <Header />
    
    <main class="main">
      <div class="container">
        <div class="category-header">
          <div class="breadcrumb">
            <span @click="$router.push('/')" style="cursor: pointer; color: #667eea;">首页</span>
            <i class="fas fa-chevron-right"></i>
            <span>{{ categoryName }}</span>
          </div>
          <h1 class="category-title">{{ categoryName }}</h1>
          <p class="category-description">{{ categoryDescription }}</p>
        </div>

        <div class="category-content">
          <aside class="category-sidebar">
            <h3 class="sidebar-title">子分类</h3>
            <div class="subcategories">
              <div 
                class="subcategory-item" 
                v-for="subcategory in subcategories" 
                :key="subcategory"
                @click="handleSubcategoryClick(subcategory)"
              >
                {{ subcategory }}
              </div>
            </div>
          </aside>

          <div class="products-section">
            <div class="products-header">
              <h2>商品列表</h2>
              <div class="products-filters">
                <select v-model="sortBy" class="filter-select">
                  <option value="default">默认排序</option>
                  <option value="price-low">价格从低到高</option>
                  <option value="price-high">价格从高到低</option>
                  <option value="sales">销量优先</option>
                  <option value="rating">评分优先</option>
                </select>
              </div>
            </div>

            <div class="products-grid">
              <div 
                class="product-card" 
                v-for="product in sortedProducts" 
                :key="product.id"
                @click="handleProductClick(product)"
              >
                <div class="product-image">
                  <img :src="product.image" :alt="product.title">
                  <div class="product-discount" v-if="(product as any).discount">-{{ (product as any).discount }}%</div>
                </div>
                <div class="product-info">
                  <div class="product-brand">{{ product.brand }}</div>
                  <h3 class="product-title">{{ product.title }}</h3>
                  <div class="product-price">
                    <span class="current-price">¥{{ product.currentPrice }}</span>
                    <span class="original-price" v-if="(product as any).originalPrice">¥{{ (product as any).originalPrice }}</span>
                  </div>
                  <div class="product-features">
                    <span class="feature-tag" v-for="feature in product.features" :key="feature">{{ feature }}</span>
                  </div>
                  <div class="product-stats">
                    <span class="sales-count">已售 {{ product.salesCount }}</span>
                    <span class="rating">
                      <i class="fas fa-star" v-for="n in 5" :key="n"></i>
                      {{ product.rating }}
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>

    <Footer />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'

const route = useRoute()
const router = useRouter()
const sortBy = ref('default')

// 分类数据映射
const categoryData = {
  'mobile-digital': {
    name: '手机 / 数码 / 电脑',
    description: '最新手机、平板、笔记本电脑，正品保障，品质保证',
    subcategories: ['手机', '平板', '笔记本', '台式机', '配件'],
    products: [
      {
        id: 1,
        brand: 'Apple',
        title: 'iPhone 15 Pro 256GB 钛金色',
        currentPrice: '8,999',
        originalPrice: '10,199',
        discount: 12,
        image: 'https://images.unsplash.com/photo-1592750475338-74b7b21085ab?w=400&h=400&fit=crop&crop=center',
        features: ['正品保障', '全国联保', '顺丰包邮'],
        salesCount: '3.2k+',
        rating: '4.9'
      },
      {
        id: 2,
        brand: 'Samsung',
        title: 'Galaxy S24 Ultra 512GB 钛灰色',
        currentPrice: '9,199',
        originalPrice: '9,999',
        discount: 8,
        image: 'https://images.unsplash.com/photo-1574944985070-8f3ebc6b79d2?w=400&h=400&fit=crop&crop=center',
        features: ['AI拍照', 'S Pen', '200MP相机'],
        salesCount: '1.8k+',
        rating: '4.7'
      },
      {
        id: 3,
        brand: 'Apple',
        title: 'MacBook Air M3 芯片 512GB 午夜色',
        currentPrice: '9,999',
        image: 'https://images.unsplash.com/photo-1517336714731-489689fd1ca8?w=400&h=400&fit=crop&crop=center',
        features: ['M3芯片', '18小时续航', '免息分期'],
        salesCount: '2.8k+',
        rating: '4.8'
      }
    ]
  },
  'fashion-sports': {
    name: '服饰 / 箱包 / 运动',
    description: '时尚服饰、精品箱包、运动装备，引领潮流风尚',
    subcategories: ['男装', '女装', '童装', '箱包', '运动鞋'],
    products: [
      {
        id: 4,
        brand: 'Nike',
        title: 'Air Max 270 React 跑步鞋 黑白配色',
        currentPrice: '899',
        image: 'https://images.unsplash.com/photo-1542291026-7eec264c27ff?w=400&h=400&fit=crop&crop=center',
        features: ['气垫科技', '透气网面', '多色可选'],
        salesCount: '4.5k+',
        rating: '4.6'
      },
      {
        id: 5,
        brand: 'Adidas',
        title: 'Ultra Boost 22 跑步鞋 白色',
        currentPrice: '1,299',
        image: 'https://images.unsplash.com/photo-1551107696-a4b0c5a0d9a2?w=400&h=400&fit=crop&crop=center',
        features: ['Boost科技', 'Primeknit鞋面', '舒适缓震'],
        salesCount: '3.7k+',
        rating: '4.5'
      }
    ]
  }
}

// 计算属性
const categoryName = computed(() => {
  const categoryPath = route.params.name as string
  return categoryData[categoryPath as keyof typeof categoryData]?.name || '分类'
})

const categoryDescription = computed(() => {
  const categoryPath = route.params.name as string
  return categoryData[categoryPath as keyof typeof categoryData]?.description || ''
})

const subcategories = computed(() => {
  const categoryPath = route.params.name as string
  return categoryData[categoryPath as keyof typeof categoryData]?.subcategories || []
})

const products = computed(() => {
  const categoryPath = route.params.name as string
  return categoryData[categoryPath as keyof typeof categoryData]?.products || []
})

const sortedProducts = computed(() => {
  let sorted = [...products.value]
  
  switch (sortBy.value) {
    case 'price-low':
      return sorted.sort((a, b) => parseInt(a.currentPrice.replace(',', '')) - parseInt(b.currentPrice.replace(',', '')))
    case 'price-high':
      return sorted.sort((a, b) => parseInt(b.currentPrice.replace(',', '')) - parseInt(a.currentPrice.replace(',', '')))
    case 'sales':
      return sorted.sort((a, b) => parseInt(b.salesCount.replace(/[^\d]/g, '')) - parseInt(a.salesCount.replace(/[^\d]/g, '')))
    case 'rating':
      return sorted.sort((a, b) => parseFloat(b.rating) - parseFloat(a.rating))
    default:
      return sorted
  }
})

// 方法
const handleSubcategoryClick = (subcategory: string) => {
  router.push(`/category/${route.params.name}/${subcategory}`)
}

const handleProductClick = (product: any) => {
  router.push(`/product/${product.id}`)
}

onMounted(() => {
  // 检查分类是否存在
  const categoryPath = route.params.name as string
  if (!categoryData[categoryPath as keyof typeof categoryData]) {
    router.push('/404')
  }
})
</script>

<style scoped>
.category-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.category-header {
  margin-bottom: 30px;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  font-size: 14px;
  color: #636e72;
}

.breadcrumb i {
  font-size: 12px;
  color: #bdc3c7;
}

.category-title {
  font-size: 32px;
  font-weight: 700;
  color: #2d3436;
  margin-bottom: 10px;
}

.category-description {
  font-size: 16px;
  color: #636e72;
  line-height: 1.6;
}

.category-content {
  display: grid;
  grid-template-columns: 250px 1fr;
  gap: 30px;
}

.category-sidebar {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  height: fit-content;
}

.sidebar-title {
  font-size: 18px;
  font-weight: 600;
  color: #2d3436;
  margin-bottom: 15px;
}

.subcategories {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.subcategory-item {
  padding: 10px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #636e72;
}

.subcategory-item:hover {
  background: #f8f9fa;
  color: #667eea;
}

.products-section {
  flex: 1;
}

.products-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.products-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #2d3436;
}

.filter-select {
  padding: 8px 12px;
  border: 1px solid #dee2e6;
  border-radius: 6px;
  background: white;
  color: #495057;
  font-size: 14px;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.product-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 30px rgba(0,0,0,0.15);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-discount {
  position: absolute;
  top: 12px;
  right: 12px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.product-info {
  padding: 20px;
}

.product-brand {
  font-size: 12px;
  color: #667eea;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.product-title {
  font-size: 16px;
  font-weight: 600;
  color: #2d3436;
  line-height: 1.4;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.current-price {
  font-size: 18px;
  font-weight: 700;
  color: #e74c3c;
}

.original-price {
  font-size: 14px;
  color: #bdc3c7;
  text-decoration: line-through;
}

.product-features {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.feature-tag {
  background: #f8f9fa;
  color: #636e72;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 500;
}

.product-stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #95a5a6;
}

.sales-count {
  font-weight: 500;
}

.rating {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 600;
}

.rating i {
  color: #f39c12;
  font-size: 10px;
}

@media (max-width: 768px) {
  .category-content {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .category-sidebar {
    order: 2;
  }
  
  .products-section {
    order: 1;
  }
  
  .products-header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
  
  .products-grid {
    grid-template-columns: 1fr;
  }
}
</style> 